﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     BaseData业务逻辑实现
    /// 作者：     lwl
    /// 日期：     2011/11/11
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
    public class BaseDataBLL
    {
        private static BaseDataBLL instance = null;
        private static object syncRoot = new object();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IBaseDataDao dao = null;

        private BaseDataBLL()
        {
            dao = BaseDataSqlMapDao.Instance;
        }

        public static BaseDataBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new BaseDataBLL();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 得到最大行数
        /// </summary>
        /// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                //daoManager.BeginTransaction();

                int count = dao.GetMaxCounts();

                //daoManager.CommitTransaction();

                return count;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.IList<BaseData> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<BaseData> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 通过DataID like 出所需要的结果集
        /// </summary>
        /// <param name="objParameters"></param>
        /// <returns></returns>
        public System.Collections.Generic.IList<BaseData> GetListByDataID(string dataID)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();
                ht.Add("DataID", dataID);
                System.Collections.Generic.IList<BaseData> list = dao.GetListByDataID(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private System.Collections.Generic.IList<BaseData> GetListByParameters(object TopCounts, BaseData basedata)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                System.Collections.Generic.IList<BaseData> list = dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过键值得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        public System.Collections.Generic.IList<BaseData> GetListByDataKey(string dataKey)
        {
            try
            {
                //daoManager.BeginTransaction();

                Hashtable ht = new Hashtable();
                ht.Add("DataKey", dataKey);

                System.Collections.Generic.IList<BaseData> list =dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 得到明细
        /// </summary>
        /// <param name="DataID"></param>
        /// <returns></returns>
        public BaseData GetDetail(System.String dataid)
        {
            try
            {
                ///daoManager.BeginTransaction();
                BaseData data = new BaseData();
                data.DataID = dataid;
                return dao.GetDetail(data);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

       
        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="basedata"></param>
        /// <returns></returns>
        public object Add(BaseData basedata)
        {
            //int id = GetId("SYS_UserInfoTemp");
            //sys_userinfotemp.UserID = id;

            try
            {
                ///daoManager.BeginTransaction();

                return dao.Add(basedata);
                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="basedata"></param>
        /// <returns></returns>
        public int Update(BaseData basedata)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Update(basedata);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="DataID"></param>
        /// <returns></returns>
        public int Delete(System.String dataid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(dataid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="basedata"></param>
        /// <returns></returns>
        public int Delete(BaseData basedata)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(basedata);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// Get All Keys
        /// </summary>
        /// <returns></returns>
        public ArrayList GetKeyList()
        {
            try
            {
                return dao.GetKeyList();
            }
            catch 
            {
                
                throw;
            }
        }

        /// <summary>
        /// 根据Key和Value获取对象
        /// </summary>
        /// <param name="p"></param>
        /// <param name="p_2"></param>
        /// <returns></returns>
        public BaseData GetDetail(string key, string value)
        {
            try
            {
                BaseData data=new BaseData();
                data.DataKey=key;
                data.DataValue=value;
                return dao.GetDetail(data);
            }
            catch 
            {
                
                throw;
            }
        }
    }
}
